package com.limetalks.logic
{
	import com.limetalks.data.Stream;
	import com.limetalks.data.User;
	import com.limetalks.events.LimeEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.net.Responder;
	
	import mx.core.Application;
	
	public class LimeTalksChat extends EventDispatcher
	{
		public var connection:NetConnection;
		
		private var LOGIN_METHOD:String = "login_Server";
		private var LOGOUT_METHOD:String = "logout_Server";
		private var START_STREAM_METHOD:String = "startStream_Server";
		private var START_AND_JOIN_STREAM_METHOD:String = "startAndJoinStream_Server";		
		private var STOP_STREAM_METHOD:String = "stopStream_Server";
		private var JOIN_STREAM_METHOD:String = "joinStream_Server";
		private var LEAVE_STREAM_METHOD:String = "leaveStream_Server";
		private var SEND_PUBLIC_MESSAGE:String = "sendPublicMessage_Server";
		private var SEND_STREAM_MESSAGE:String = "sendStreamMessage_Server";
		
		// declare variables
		var ip:String = "localhost";
		var port:String = "";
		var app:String = "LimeTalksRed5";
		var connectionUrl:String = "rtmp://" + ip + "/" + app + "/video/";
		
		public function connect(url:String="rtmp://localhost/LimeTalksRed5/video/") : void
		{
			// change to private and move to loadConfig method
			// setup connection code	
			connection = new NetConnection();
			connection.addEventListener(NetStatusEvent.NET_STATUS, onConnectionNetStatus);
			connection.client = this;
			connection.connect(url);
		}
		
		public function disconnect() : void
		{
			connection.close();
		}
		
		public function login(sessionId:String) : void
		{
			connection.call(LOGIN_METHOD, new Responder(function(){}), sessionId);
		}
		
		public function logout() : void
		{
			connection.call(LOGOUT_METHOD, new Responder(function(){}));
		}
		
		public function startStream(streamId:String) : void 
		{
			connection.call(START_STREAM_METHOD, new Responder(function(){}), streamId);
		}
		
		public function joinStream(streamId:String) : void 
		{
			connection.call(JOIN_STREAM_METHOD, new Responder(function(){}), streamId);
		}
		
		public function startAndJoinStream(streamId:String) : void 
		{
			connection.call(START_AND_JOIN_STREAM_METHOD, new Responder(function(){}), streamId);
		}
		
		public function leaveStream(streamId:String) : void 
		{
			connection.call(LEAVE_STREAM_METHOD, new Responder(function(){}), streamId);
		}
		
		public function stopStream(streamId:String) : void 
		{
			connection.call(STOP_STREAM_METHOD, new Responder(function(){}), streamId);
		}		
		
		public function sendPublicMessage(message:String) : void 
		{
			connection.call(SEND_PUBLIC_MESSAGE, new Responder(function(){}), message);
		}
		
		public function sendStreamMessage(message:String) : void 
		{
			connection.call(SEND_STREAM_MESSAGE, new Responder(function(){}), message);
		}
		
		
		private function onConnectionNetStatus(event:NetStatusEvent) : void 
		{
//			log(event.info.code);			
			var e:LimeEvent = new LimeEvent(LimeEvent.CONNECTION);
			// did we successfully connect
			if(event.info.code == "NetConnection.Connect.Success") {	
				e.params.success = true;				
			}else{
				e.params.success = false;
			}
			dispatchEvent(e);
		}
				
		//client functions
		public function onStreamStarted_Client(stream:String):void
		{
			var e:LimeEvent = new LimeEvent(LimeEvent.STREAM_START);
			e.params.stream = stream;
			dispatchEvent(e);
		}
		
		public function onStreamStopped_Client(stream:String):void
		{
			var e:LimeEvent = new LimeEvent(LimeEvent.STREAM_STOP);
			e.params.stream = stream;
			dispatchEvent(e);
		}
		
		public function onStreamUpdated_Client(stream:String):void
		{
//			log("Stream updated: " + stream.name);		
			var e:LimeEvent = new LimeEvent(LimeEvent.STREAM_UPDATE);
			e.params.stream = stream;
			dispatchEvent(e);
		}
		
		public function onStreamJoin_Client(user:String, stream:String):void
		{
//			log("User '" + user.name + "' has joined the stream '" + stream.name + "'");
			var e:LimeEvent = new LimeEvent(LimeEvent.STREAM_JOIN);
			e.params.stream = stream;
			e.params.user = user;
			dispatchEvent(e);
		}
		
		public function onStreamLeave_Client(user:String, stream:String):void
		{
//			log("User '" + user.name + "' has left the stream '" + speach.name + "'");
			var e:LimeEvent = new LimeEvent(LimeEvent.STREAM_LEAVE);
			e.params.stream = stream;
			e.params.user = user;
			dispatchEvent(e);
		}
		
		public function onUserLogin_Client(user:String):void
		{
			var e:LimeEvent = new LimeEvent(LimeEvent.LOGIN);
			e.params.user = user;
			dispatchEvent(e);
		}
		
		public function onUserLoginError_Client():void
		{
			//			log("User '" + user.name + "' has left the stream '" + speach.name + "'");
			var e:LimeEvent = new LimeEvent(LimeEvent.LOGIN_ERROR);
			dispatchEvent(e);
		}
		
		public function onUserLogout_Client(user:String):void
		{
			//						log("User '" + user.name + "' has left the stream '" + speach.name + "'");
			var e:LimeEvent = new LimeEvent(LimeEvent.LOGOUT);
			e.params.user = user;
			dispatchEvent(e);
		}
	}
}
